package api

import (
	"fmt"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"
	"time"

	"github.com/labstack/echo/v4"
)

func (base *Controller) GameList(c echo.Context) error {
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.List(ctx, &pb.Empty{})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) Stages(c echo.Context) error {
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.Stages(ctx, &pb.Empty{})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) Signin(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.Signin(ctx, &pb.SigninReq{
		UserId: int64(user.Id),
	})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) SigninStage(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.SigninStage(ctx, &pb.SigninStageReq{
		UserId: int64(user.Id),
	})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) SigninHitory(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.SigninHistory(ctx, &pb.SigninHistoryReq{
		UserId: int64(user.Id),
	})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) SigninTest(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	global.GetRds().Set(fmt.Sprintf("DAILY_CHANCE_%d_%s", user.Id, time.Now().Format("2006-01-02")), 100, time.Hour*24)

	return util.BuildSuccess(c, nil, "")
}

func (base *Controller) FruitGameConfig(c echo.Context) error {
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	user, _ := base.GetUserByCtx(c)
	resp, _ := grpc_client.GrpcGameServer.FruitConfig(ctx, &pb.FruitConfigReq{UserId: int64(user.Id)})
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) FruitGameAction(c echo.Context) error {
	req := &pb.FruitActionReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	req.UserId = int64(user.Id)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.FruitAction(ctx, req)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 掷色子游戏
// @Description 掷色子游戏
// @Accept  json
// @Produce  json
// @Tags game
// @Param   sendBody body protobuf.DiceActionReq true "body"
// @Success 200 {object} protobuf.DiceActionResp "result"
// @Router /api/game/dice_game_action [POST]
func (base *Controller) DiceGameAction(c echo.Context) error {
	req := &pb.DiceActionReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	req.UserId = int64(user.Id)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.DiceAction(ctx, req)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 掷色子游戏历史
// @Description 掷色子游戏历史
// @Accept  json
// @Produce  json
// @Tags game
// @Param   sendBody body protobuf.DiceHistoryReq true "body"
// @Success 200 {object} protobuf.DiceHistoryResp "result"
// @Router /api/game/dice_game_history [POST]
func (base *Controller) DiceGameHistory(c echo.Context) error {
	req := &pb.DiceHistoryReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	req.UserId = int64(user.Id)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcGameServer.DiceHistory(ctx, req)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}
